<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const loginForm = reactive({
  username: '',
  password: '',
  remember: false
})

const loading = ref(false)
const loginError = ref('')

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const formRef = ref()

const handleLogin = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid: boolean) => {
    if (valid) {
      loading.value = true
      loginError.value = ''
      
      try {
        // 这里应该调用实际的登录 API
        // 模拟登录成功
        setTimeout(() => {
          const userData = {
            id: '1',
            name: loginForm.username,
            avatar: 'https://placeholder.pics/svg/100x100',
            token: 'mock-token-' + Date.now()
          }
          
          userStore.login(userData)
          
          // 如果有重定向，则跳转到重定向页面
          const redirectPath = route.query.redirect as string
          router.push(redirectPath || '/')
          
          loading.value = false
        }, 1000)
      } catch (error) {
        console.error('Login failed:', error)
        loginError.value = '登录失败，请检查用户名和密码'
        loading.value = false
      }
    }
  })
}
</script>

<template>
  <div class="login-container min-h-screen flex-center bg-gray-50">
    <el-card class="login-card w-full max-w-md">
      <template #header>
        <div class="text-center">
          <h2 class="text-2xl font-bold text-primary">信息门户系统</h2>
          <p class="text-gray-500 mt-2">欢迎登录</p>
        </div>
      </template>
      
      <el-form 
        ref="formRef"
        :model="loginForm" 
        :rules="rules"
        label-position="top"
      >
        <el-alert
          v-if="loginError"
          :title="loginError"
          type="error"
          show-icon
          class="mb-4"
        />
        
        <el-form-item label="用户名" prop="username">
          <el-input 
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        
        <div class="flex-between mb-4">
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
          <el-link type="primary" href="#">忘记密码?</el-link>
        </div>
        
        <el-button 
          type="primary" 
          class="w-full"
          :loading="loading"
          @click="handleLogin"
        >
          登录
        </el-button>
        
        <div class="text-center mt-4">
          <span class="text-gray-500">还没有账号?</span>
          <router-link to="/register" class="text-primary ml-1">立即注册</router-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<style lang="less">
.login-card .el-card__header {
  padding-bottom: 0;
}
</style>
